package com.key.puzzlemodel.transverselongitudinal.fragment

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.key.puzzlemodel.R
import com.key.puzzlemodel.base.BaseMenuFragment
import com.key.puzzlemodel.bean.ColorBgBean
import com.key.puzzlemodel.databinding.FragmentPuzzleBackgroundBinding
import com.key.puzzlemodel.databinding.ItemPuzzleColorBinding
import com.key.puzzlemodel.transverselongitudinal.IPuzzleInterFace
import com.key.puzzlemodel.transverselongitudinal.TransverseLongitudinalMVVMViewModel

/**
 *
 * @功能描述:     背景
 * @创建时间:     2022/1/10
 * @创建者:       阳
 * @修改时间:     2022/1/10
 * @修改者:       阳
 */
class BackgroundFragment :
    BaseMenuFragment<FragmentPuzzleBackgroundBinding, TransverseLongitudinalMVVMViewModel>(),
    View.OnClickListener {

    override val layoutId: Int
        get() = R.layout.fragment_puzzle_background


    var colorBg: Int = -1
    var itemPosition = -1

    var backgroundAdapter: BackgroundAdapter? = null


    override fun onAttach(context: Context) {
        super.onAttach(context)
        puzzleInterface = context as IPuzzleInterFace
    }


    /**
    1、点击背景，底部弹出背景色快，点击色块直接将背景色改成选中的颜色。
    2、点击色值吸管，如右图，显示取色器图标，单指拖动取色器图标吸取内容板块任意颜色，拖动时取色器环根据不同的目标色值动态显示颜色，松手即选中吸取的色值显示到背景，并隐藏取色器图标。
     */
    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this

        initRecyclerView()
    }


    private fun initRecyclerView() {
        val colorBgList = ArrayList<ColorBgBean>()

        val colorStrings =
            "#ffffffff,#ff000000,#ffd1d1d1,#ff5d5d5d,#fffa0000,#ffffa8a2,#ffff6c00,#ffffb47c,#ffffb100," +
                    "#ffffd471,#ff0ad477,#ff85ebb8,#ff0be4f5,#ff036ae2,#ff1abeff,#ff6f4efe,#ffac7cff,#fff689ff,#ff9f00d9,#ffed3172,#ffff6fa6,#fffeafc4"
        var colorArray = colorStrings.split(",") as ArrayList<String>

        colorBgList.clear()
        for (color in colorArray) {
            colorBgList.add(ColorBgBean(color))
        }

        mVDB.recyclerColorContent.layoutManager =
            LinearLayoutManager(requireActivity(), LinearLayoutManager.HORIZONTAL, false)

        backgroundAdapter =
            BackgroundAdapter(requireActivity(), colorBgList, object : BackgroundAdapter.ItemClick {
                override fun clickResult(position: Int, bean: ColorBgBean) {
                    colorBg = Color.parseColor(bean.color)
                    setRecyclerViewBg(colorBg, position)
                }
            })
        mVDB.recyclerColorContent.adapter = backgroundAdapter

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.img_background_close -> {
                colorBg = mVM.backgroundColor

                itemPosition = mVM.backgroundItemIndex
                updateAdapter(mVM.backgroundItemIndex)

                //重置之前的颜色
                puzzleInterface.clickColorBackground(mVM.backgroundColor)
                //关闭
                puzzleInterface.clickStartFragment(mVM.MENUTYPE)
            }
            R.id.img_background_ok -> {
                mVM.backgroundColor = colorBg
                //更新选中的值
                mVM.backgroundItemIndex = itemPosition

                puzzleInterface.clickStartFragment(mVM.MENUTYPE)
            }
            R.id.iv_color_picker -> {
                puzzleInterface.showClickPicker()
            }
        }
    }


    fun setRecyclerViewBg(bgColor: Int, position: Int) {
        //设置缓存熟悉
        colorBg = bgColor
        itemPosition = position
        //更新adapter
        updateAdapter(itemPosition)
        //执行回调
        puzzleInterface.clickColorBackground(bgColor)
    }

    /**
     * 更新adatper的数据
     */
    private fun updateAdapter(position: Int) {
        if (backgroundAdapter != null) {
            for (index in 0 until backgroundAdapter!!.colorList!!.size) {
                backgroundAdapter!!.colorList[index].isselect = false
            }

            if (position != -1) {
                backgroundAdapter!!.colorList[position].isselect = true
            }

            backgroundAdapter!!.notifyDataSetChanged()
        }
    }

    class BackgroundAdapter(
        val context: Context,
        val colorList: ArrayList<ColorBgBean>,
        private val itemListener: ItemClick? = null,
    ) :
        RecyclerView.Adapter<BackgroundAdapter.ViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(
                DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context),
                    R.layout.item_puzzle_color,
                    parent,
                    false
                )
            )
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val bean: ColorBgBean = colorList[position]

            if (bean.color == "#ffffffff") {
                val drawable = ContextCompat.getDrawable(
                    context,
                    R.drawable.shape_puzzle_color,
                ) as GradientDrawable

                drawable.setColor(Color.parseColor(bean.color))
                holder.itemBinding.puzzleColor.background = drawable
            } else {
                holder.itemBinding.puzzleColor.setBackgroundColor(Color.parseColor(bean.color))
            }
            holder.itemBinding.flContent.isSelected = bean.isselect

            holder.itemBinding.flContent.setOnClickListener {
                itemListener?.clickResult(position, bean)
            }

        }

        override fun getItemCount(): Int {
            return colorList.size
        }

        inner class ViewHolder(val itemBinding: ItemPuzzleColorBinding) :
            RecyclerView.ViewHolder(itemBinding.root) {
        }


        interface ItemClick {
            fun clickResult(position: Int, bean: ColorBgBean)
        }

    }


}